package udes.assassin;

import udes.assassin.R;
import udes.assassin.server.api.Message.Message;
import udes.assassin.server.api.Message.LobbyMessage.LeavingMessage;
import udes.assassin.server.api.Message.RulesMessage.GameEndMessage;
import udes.assassin.server.api.Message.RulesMessage.InGameUpdateMessage;
import udes.assassin.structure.Player;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class LeaderboardActivity extends Activity implements OnClickListener, MessageManager{

	private ArrayAdapter<Player> mAdapter;
	private AssassinApplication mAssassin;
	private ApplyMessageTask mTask;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leaderboard);
        
      	((Button) findViewById(R.id.button_quit_game)).setOnClickListener(this);
      	
      	mAssassin = (AssassinApplication) getApplication();
        mAssassin.mCurrentManager = this;
    }
    
    @Override 
    public void onResume(){
    	super.onResume();
    	
    	// Update info on page
    	updateLeaderboard();
    	
    	if(mTask != null){
    		mTask.stop();
    	}
    	mTask = new ApplyMessageTask(this, mAssassin);
        mTask.execute(1);
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		mTask.stop();
	}
    
    public void onClick(View view) {
        Button clickedBtn = (Button) view;

        switch (clickedBtn.getId()) {
        	case R.id.button_quit_game: 
        		goHomePage();
                break;
        }
    }
    
    public void onBackPressed (){
    	goHomePage();
    }
    
    private void goHomePage(){

		mAssassin.sendMessage(new LeavingMessage());
		mAssassin.sendMessage(new LeavingMessage());
    	
    	Intent intent = new Intent( this, MainActivity.class );
    	intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
    	mTask.stop();
    	finish();
    	startActivity(intent);
    }

	public boolean manage(Message aMessage) {
		if(aMessage.getClass().equals(GameEndMessage.class)) {
			GameEndMessage casted = (GameEndMessage)aMessage;
			Global.getInstance().mGame.mWinnerName = casted.winnerName;
			updateLeaderboard();
			return true;
		}
		else if(aMessage.getClass().equals(InGameUpdateMessage.class)) {
			InGameUpdateMessage casted = (InGameUpdateMessage)aMessage;
			Global.getInstance().mGame.updatePlayerInfo(casted.users);
			updateLeaderboard();
			return true;
		}
		return false;
	}
	
	public void updateLeaderboard(){
		/*TextView numberOfPlayer = ((TextView) findViewById(R.id.label_remaining_player_name));
		numberOfPlayer.setText(Global.getInstance().mGame.mWinnerName);
		*/
		mAdapter = new ScoreListAdapter(this, Global.getInstance().mGame.getPlayers());
      	ListView lv = ((ListView) findViewById(R.id.leaderboard_player_list));
      	lv.setAdapter(mAdapter);
      	mAdapter.notifyDataSetChanged();
	}
}
